package web.action.com;


import com.fz.us.base.dao.jdbc.JdbcDao;
import com.joint.base.service.FileManageService;
import com.joint.base.service.UsersService;
import com.mongodb.gridfs.GridFSDBFile;
import org.apache.commons.lang3.StringUtils;
import org.apache.struts2.convention.annotation.ParentPackage;
import org.bson.types.ObjectId;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.data.mongodb.core.query.Query;
import org.springframework.data.mongodb.gridfs.GridFsTemplate;
import web.action.BaseAdminAction;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.io.OutputStream;
import java.io.UnsupportedEncodingException;
import java.util.HashMap;
import java.util.Map;

/**
 * 后台Action类 - 网站注册 - 登录注册
 * ============================================================================
 * 版权所有 2013 min_xu。
 * ----------------------------------------------------------------------------
 *
 * @author min_xu
 * @version 0.1 2013-7-15
 */

@ParentPackage("com")
public class FileAction extends BaseAdminAction {
    private static final long serialVersionUID = -5383463207248344967L;

    @Resource
    private UsersService usersService;
    @Resource
    private FileManageService fileManageService;
    @Resource
    private JdbcDao jdbcDao;
    @Resource
    private GridFsTemplate gridOperations;
    private String gridId;

    public String execute() throws IOException {
        GridFSDBFile file = null;
        if (StringUtils.isNotEmpty(keyId)) {
            if (fileManageService.getFileByKeyId(keyId).size() > 0) {
                file = gridFSGet(fileManageService.getFileByKeyId(keyId).get(0).getGridId());
            } else {
                file = gridFSGet(keyId);
            }
        } else if (StringUtils.isNotEmpty(gridId)) {
            file = gridFSGet(fileManageService.get("gridId", gridId).getGridId());
        }
        if (file == null) {
            return null;
        }
        OutputStream out = null;
        HttpServletResponse response = getTheResponse();
        response.setContentType(file.getContentType());
        try {
            response.setHeader("Content-Disposition", "attachment; filename=\"" + new String(file.getFilename().getBytes(), "ISO8859-1") + "\"");
        } catch (UnsupportedEncodingException e) {
            e.printStackTrace();
        }

        out = response.getOutputStream();
        file.writeTo(out);

        return null;
    }

    //
    public String remove() throws IOException {
        if (StringUtils.isNotEmpty(keyId)) {
            GridFSDBFile file = gridFSGet(keyId);
            String fileId = file.getMetaData().get("fileId").toString();

            if (StringUtils.isNotEmpty(fileId)) {
                String sql = "delete from ss_product_ss_filemanage where picsUrl_id = :fileId ";

                Map<String, String> filter = new HashMap<String, String>();
                filter.put("fileId", fileId);
                try {
                    @SuppressWarnings("unchecked")
                    int i = jdbcDao.executeForMap(sql, filter);
                } catch (Exception e) {
                    e.printStackTrace();
                }

                String sql2 = "update ss_filemanage set state = 'Delete' where id = :fileId ";
                Map<String, String> filter2 = new HashMap<String, String>();
                filter2.put("fileId", fileId);
                try {
                    @SuppressWarnings("unchecked")
                    int i = jdbcDao.executeForMap(sql2, filter2);

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }

            gridOperations.delete(new Query(Criteria.where("_id").is(
                    new ObjectId(keyId))));
        }

        return ajaxHtmlCallback("200", "选项已删除成功！", "操作成功");
    }

    public String getGridId() {
        return gridId;
    }

    public void setGridId(String gridId) {
        this.gridId = gridId;
    }
}